home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
BARNET
/
FREENET
/
WWW
/
LIST.C
next >
Wrap
C/C++ Source or Header
|
1998-05-07
|
15KB
|
534 lines
/***************************************************************************/
/* */
/* Program : to create various index files in both txt and html format */
/* */
/* */
/* Author : C.Carpenter */
/* Date : 18/04/98 */
/* */
/***************************************************************************/
/***************************************************************************/
/* includes */
/***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/***************************************************************************/
/* defines */
/***************************************************************************/
#define FALSE 0
#define TRUE 1
#define MAX_FILENAME 256
#define MAX_EXCLUDES 50
#define MAX_FILES 1000
/*#ifdef __riscos
#define strcasecmp stricmp
#endif
*/
/***************************************************************************/
/* global variables */
/***************************************************************************/
typedef struct
{
char filename[MAX_FILENAME];
} exclude_rec;
exclude_rec excludes[MAX_EXCLUDES];
int total_excludes;
typedef struct
{
char rights[11];
char val[2];
char user[10];
char group[10];
char size[10];
char dayword[4];
char month[4];
char daynum[3];
char hour[3];
char min[3];
char sec[3];
char year[5];
char filename[MAX_FILENAME];
char directory[MAX_FILENAME];
} rec;
rec record[MAX_FILES];
int indx[MAX_FILES];
int total_files;
char d[20];
/***************************************************************************/
/* function names */
/***************************************************************************/
int readData(char *filename);
int readExcludes(void);
int checkExcludes(char *filename);
int createAlphaList(char *fname);
int createAuthorList(char *fname);
int createLatestChanges(char *filename);
int writeHTMLHeader(char *filename, char *title, char *header);
int writeHTMLFooter(char *filename);
/***************************************************************************/
/* main */
/***************************************************************************/
int main(int argc, char *argv[])
{
/* Check for the right number of parameters */
if (argc!=3)
{
printf("Usage: list <filein> <fileout>\n");
return(1);
}
/* read in the excludes file */
if ((total_excludes = readExcludes()) <0)
{
printf("Unable to open excludes file\n");
return(1);
}
total_files = readData(argv[1]);
if (total_files > 0)
{
createAlphaList(argv[2]);
createAuthorList(argv[2]);
// createLatestChanges(argv[2]);
}
return(0);
}
/***************************************************************************/
/* readData */
/***************************************************************************/
int readData(char *filename)
{
int count=0, k=0;
char directory[MAX_FILENAME];
FILE *handle;
char buffer[256];
/* Make sure we can open the in file */
if ((handle=fopen(filename, "r"))==NULL)
{
printf("Unable to open file '%s' for reading\n", filename);
exit(1);
}
/* Read in the data from the ls -lR list into an array */
count = 0;
directory[0]='\0';
while (!feof(handle))
{
/* Read the next line */
fgets(buffer, sizeof(buffer), handle);
if ((strrchr(buffer, ':')!=NULL) && (strchr(buffer, ' ')==NULL))
{
strncpy(directory, buffer, 256);
directory[255]='\0';
directory[strlen(directory)-2]='\0'; // Get rid of the ':'
}
else
{
if (strlen(buffer)>1)
{
sscanf(buffer, "%10s %1d %8s %8s %9s %3s %3s %2s %2s:%2s:%2s %4s %s",
record[count].rights,
record[count].val,
record[count].user,
record[count].group,
record[count].size,
record[count].dayword,
record[count].month,
record[count].daynum,
record[count].hour,
record[count].min,
record[count].sec,
record[count].year,
record[count].filename );
strcpy(record[count].directory, directory);
k=atoi(record[count].size);
k=k/1024;
if (k!=0)
{
sprintf(record[count].size, "%dk", k);
}
if (!checkExcludes(record[count].filename))
{
indx[count]=count;
count ++;
}
}
}
}
fclose(handle);
return(count);
}
/***************************************************************************/
/* createAlphaList */
/***************************************************************************/
int createAlphaList(char *fname)
{
int swapped, speedup, loop, tmp;
FILE *handle;
char filename[MAX_FILENAME], buffer[256];
/* Sort the list */
swapped = TRUE;
speedup = 0;
while (swapped==TRUE)
{
swapped=0;
for (loop=0; loop<total_files-1-speedup; loop++)
{
if (strcasecmp(record[indx[loop]].filename, record[indx[loop+1]].filename) >0 )
{
tmp = indx[loop];
indx[loop] = indx[loop+1];
indx[loop+1] = tmp;
swapped=TRUE;
}
}
speedup ++;
}
/* Write out the sorted list to the file */
sprintf(filename, "%s-alpha-list.html", fname);
// strcpy(filename, "alpha/htm");
/* Make sure we can open the html file */
if ((handle=fopen(filename,"w"))==NULL)
{
printf("Unable to open file '%s'\n", filename);
return(-1);
}
fclose(handle);
/* Create the header */
sprintf(buffer, "%s Index in Alphabetical order of filename", fname);
writeHTMLHeader(filename, buffer, buffer);
/* for (loop=0; loop<count; loop++)
{
fprintf(handle, "%-40s %-10s %2s:%2s %2s %3s %4s %s\n",
record[indx[loop]].filename,
record[indx[loop]].size,
record[indx[loop]].hour,
record[indx[loop]].min,
record[indx[loop]].daynum,
record[indx[loop]].month,
record[indx[loop]].year,
record[indx[loop]].directory);
}
fclose(handle);
*/
/* Make sure we can open the html file */
if ((handle=fopen(filename,"a"))==NULL)
{
printf("Unable to open file '%s'\n", filename);
return(-1);
}
fprintf(handle, "<table border=0>\n");
for (loop=0; loop<total_files; loop++)
{
sprintf(buffer, "%-25s", record[indx[loop]].filename);
buffer[20]='\0';
fprintf(handle, "<tr><td>%-25s<td align=right>%.9s<td>%2s:%2s %2s %3s %4s",
buffer,
record[indx[loop]].size,
record[indx[loop]].hour,
record[indx[loop]].min,
record[indx[loop]].daynum,
record[indx[loop]].month,
record[indx[loop]].year);
sprintf(buffer, "%s/%s",
record[indx[loop]].directory,
record[indx[loop]].filename);
if (strlen(buffer)>27)
strcpy(buffer+27,"...\0");
fprintf(handle, "<td><a href=\"%s/%s\">%s</a>",
record[indx[loop]].directory,
record[indx[loop]].filename,
buffer);
fprintf(handle,"\n");
}
fprintf(handle, "</table>\n");
fclose(handle);
writeHTMLFooter(filename);
return(0);
}
/***************************************************************************/
/* createAuthorList */
/***************************************************************************/
int createAuthorList(char *fname)
{
char filename[MAX_FILENAME];
char buffer[256], olddir[256];
FILE *handle;
int loop;
sprintf(filename, "%s-author-list.html", fname);
// strcpy(filename, "author/htm");
/* Make sure we can open the html file */
if ((handle=fopen(filename,"w"))==NULL)
{
printf("Unable to open file '%s'\n", filename);
return(-1);
}
fclose(handle);
/* Create the header */
sprintf(buffer, "%s Index by Author", fname);
writeHTMLHeader(filename, buffer, buffer);
/* Make sure we can open the html file */
if ((handle=fopen(filename,"a"))==NULL)
{
printf("Unable to open file '%s'\n", filename);
return(-1);
}
olddir[0]='\0';
fprintf(handle, "<ul>\n");
for (loop=0; loop<total_files; loop++)
{
if (strcmp(olddir, record[loop].directory) != 0)
{
/* new directory name */
if (loop>0)
{
fprintf(handle, "</ul>\n");
fprintf(handle, "</ul>\n");
}
fprintf(handle, "<ul>\n");
strcpy(olddir, record[loop].directory);
fprintf(handle, "<li>Directory: <a href=\"%s\">%s</a></li>\n",
record[loop].directory, record[loop].directory);
fprintf(handle, "<ul>\n");
}
sprintf(buffer, "%-20s", record[indx[loop]].filename);
buffer[20]='\0';
fprintf(handle, "<li>%2s:%2s %2s %3s %4s",
record[loop].hour,
record[loop].min,
record[loop].daynum,
record[loop].month,
record[loop].year);
sprintf(buffer, "%s", record[loop].filename);
if (strlen(buffer)>27)
strcpy(buffer+27,"...\0");
fprintf(handle, "<a href=\"%s/%s\">%s</a>(%s)</li>",
record[loop].directory,
record[loop].filename,
buffer,
record[loop].size);
fprintf(handle,"\n");
}
fprintf(handle, "</ul>\n");
fprintf(handle, "</ul>\n");
fclose(handle);
writeHTMLFooter(filename);
return(0);
}
/***************************************************************************/
/* createLatestChanges */
/***************************************************************************/
int createLatestChanges(char *filename)
{
return(0);
}
/***************************************************************************/
/* checkExcludes */
/***************************************************************************/
int checkExcludes(char *text)
{
int loop;
for (loop=0; loop<total_excludes; loop++)
{
if (strstr(text, excludes[loop].filename)!=NULL)
{
// printf("%s", text);
return(TRUE);
}
}
return(FALSE);
}
/***************************************************************************/
/* readExcludes */
/***************************************************************************/
int readExcludes(void)
{
FILE *handle;
char tmp[80];
int count=0;
if ((handle = fopen("excludes", "r"))==NULL)
{
return(FALSE);
}
count=0;
while (!feof(handle) & (count < MAX_EXCLUDES))
{
fgets(tmp, sizeof(tmp), handle);
if (strlen(tmp)>1)
{
tmp[strlen(tmp)-1]='\0';
strncpy(excludes[count].filename, tmp, sizeof(excludes[count].filename));
excludes[count].filename[MAX_FILENAME-1]='\0';
count++;
}
}
count--;
total_excludes=count;
fclose(handle);
return(TRUE);
}
/***************************************************************************/
/* writeHTMLHeader */
/***************************************************************************/
int writeHTMLHeader(char *filename, char *title, char *header)
{
FILE *handle;
if ((handle=fopen(filename, "w")) == NULL)
{
printf("Unable to open file '%s' for writing\n", filename);
return(-1);
}
fprintf(handle, "<html><head><title>%s</title></head>\n", title);
fprintf(handle, "<body background=\"www/images/bg.gif\">\n");
fprintf(handle, "<h2 align=center>%s</h2>\n", header);
fprintf(handle, "<p>This file is automatically generated each night by this <a href=\"www/list.c\">C source
program.</a>\n");
fprintf(handle, "<hr>\n");
fclose(handle);
return(0);
}
/***************************************************************************/
/* wrtieHTMLFooter */
/***************************************************************************/
int writeHTMLFooter(char *filename)
{
FILE *handle;
if ((handle=fopen(filename, "a")) == NULL)
{
printf("Unable to open file '%s' for appending\n", filename);
return(-1);
}
fprintf(handle, "<hr>\n");
// fprintf(handle, "<p>Automatically generated at %s</p>\n", d);
fprintf(handle, "<p><a href=\"mailto:ftpadmin@barnet.ac.uk\">ftpadmin@barnet.ac.uk</a></p>\n");
fprintf(handle, "</body></html>\n");
fclose(handle);
return(0);
}